<%@ page language="java"
	import="java.util.*" pageEncoding="utf8"%>
<%@ page
	import="org.apache.commons.fileupload.DiskFileUpload,java.text.SimpleDateFormat,org.apache.commons.fileupload.FileItem,java.io.File"%>
<%@ page
	import="org.apache.commons.fileupload.FileUploadException,org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException"%>

<%

		String  tproid = "630";
		int useid = 231;


		String spath = "";

	final int MAX_SIZE = 10 * 30000 * 1024;// 设置上传文件最大为 300k
	final int ONE_MAX_SIZE=300 * 1024;
	        // 允许上传的文件格式的列表 
	   final String[] allpict = new String[] { ".jpg", ".jpeg", ".gif",".jpeg",".png",".bmp",".JPG", ".JPGE", ".GIF",".JPEG",".PNG",".BMP" };
  	 
	   String path2 =   request.getSession().getServletContext().getRealPath("/kissy");	
	   System.out.println("path2:"+path2);
	   DiskFileUpload fu = new DiskFileUpload();
        // 设置最大文件尺寸，这里是4MB
//        fu.setSizeMax(41943040);
        fu.setSizeMax(MAX_SIZE);
   //     System.out.println("fu="+fu);
        // 设置缓冲区大小，这里是4kb
        fu.setSizeThreshold(MAX_SIZE);
        // 设置临时目录：
        fu.setRepositoryPath(path2+"\\temp");
        // 得到所有的文件：
        List fileItems;
        String message = "";
        //生成当前时间 的
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMddHHmmssS");
        String tday = formater.format(new Date());
        
        useid=useid+10041235;
        tday=tday.substring(0, 8)+useid+tday.substring(8,16);
	
        
		int it=1;
		List<String> ls = new ArrayList<String>();
		Boolean namenull=false;

		try {
        fileItems = fu.parseRequest(request);
        
        Iterator iter = fileItems.iterator();
        while(iter.hasNext()){
        FileItem item = (FileItem) iter.next();
        
         if (!item.isFormField()) {
         String name = item.getName();
         long size = item.getSize();
         if((name==null||name.equals("")) && size==0){
        //	 message="succ";
        	 namenull=true;
        	 continue;
         }
        
         String tnam = name.substring(name.lastIndexOf("."), name.length());
           boolean picge = false;	
         for(String sin:allpict){
        	 if(tnam.equals(sin)){
        		 picge=true;
        	 }
         }if(!picge){
         	message="1";

}else{		
	if(size>ONE_MAX_SIZE){
        	  message="2";
         	}

         
         it++;
         //返回文件名，以时间为名，路径：e:/temp
			String contextPath = request.getContextPath();
		        String path  = path2+"/pic/"+tday.substring(0,6)+"/"+tday.substring(6,8)
		 		           +"/"+tday+it+tnam;
		        String savpath  = path2+"/pic/"+tday.substring(0,6)+"/"+tday.substring(6,8)
		         +"/308x308_"+tday+it+tnam;
		 		File file = new File(path);
		spath =contextPath+"/kissy/pic/"+tday.substring(0,6)+"/"+tday.substring(6,8)
		 		           +"/"+tday+it+tnam;
		System.out.println("spath="+spath);
		 		boolean b = file.getParentFile().isDirectory();
		 		if(!b){
		 	file.getParentFile().mkdirs();//创建目录
		 		}
		         
		         try {
		            item.write(file);
		            float p = 308;

		            String picpathna=tday+it+tnam;
		           
		         
		        
		} catch (Exception e) {
		          System.out.println(e.getMessage());
		            message = "5";
	    }
}         
	      }
	      
	    }
	   } catch (FileUploadException e) {
		   if (e instanceof SizeLimitExceededException) { 
               message="2";

           }  
	   }
	System.out.println("[\"0\", \""+spath+"\"]");
	out.println("[\"0\", \""+spath+"\"]"); 

%>


